Skip to content

fix: resolve SonarQube S1192 and S3776 issues in discussions.go#87

Open
devin-ai-integration[bot] wants to merge 1 commit intomainfrom
devin/1770923769-fix-sonarqube-discussions
Open

fix: resolve SonarQube S1192 and S3776 issues in discussions.go#87
devin-ai-integration[bot] wants to merge 1 commit intomainfrom
devin/1770923769-fix-sonarqube-discussions

Conversation

@devin-ai-integration
Copy link

Closes: SonarQube issues go:S1192 (String literals should not be duplicated) and go:S3776 (Cognitive Complexity too high) in pkg/github/discussions.go.

Summary

Addresses two High-severity SonarQube findings in discussions.go, scoped to a single file change:

S1192 – Duplicated string literals: Extracted four repeatedly-used literals into package-level constants (descRepoOwner, descRepoName, errGQLClientFmt, categoryLabelFmt). Each was duplicated 3–4× across the four tool functions in this file.

S3776 – Cognitive Complexity of ListDiscussions (27 → well under 15): The original function contained two near-identical branches (with/without category filter), each with inline struct definitions, query execution, and node-to-Issue mapping. Refactored by extracting:

  • discussionNode – shared struct type for GraphQL response nodes
  • queryDiscussionsWithCategory / queryDiscussionsWithoutCategory – query helpers
  • mapDiscussionNodesToIssues – shared mapping logic

Important review items

  1. Behavioral change on empty results: The original code returned null JSON when no discussions were found (nil slice via var discussions []*github.Issue). The new code returns [] (empty slice via make). This is arguably more correct but is a subtle contract change worth verifying downstream consumers can handle.
  2. GraphQL struct tag propagation: Confirm the extracted discussionNode type with its graphql:"category" and graphql:"url" tags is correctly resolved by the githubv4 client when used inside the wrapper query structs in queryDiscussionsWithCategory/queryDiscussionsWithoutCategory.
  3. Constant naming scope: descRepoOwner, descRepoName, etc. are unexported (lowercase) and local to the github package. If other files in this package use the same literals, these constants could be reused — but this PR intentionally limits changes to one file.

Tradeoffs

  • Two separate query helper functions (queryDiscussionsWithCategory / queryDiscussionsWithoutCategory) were kept instead of a single parameterized function because the GraphQL query struct tags differ (discussions(first: 100, categoryId: $categoryId) vs discussions(first: 100)), making compile-time struct unification non-trivial.

Link to Devin run: https://app.devin.ai/sessions/cb8670bcf6e74341a4ed0784765549ec
Requested by: Shannon Hittson (@shannonhittson-eng)

- S1192: Extract duplicated string literals into constants (descRepoOwner,
  descRepoName, errGQLClientFmt, categoryLabelFmt)
- S3776: Reduce cognitive complexity of ListDiscussions by extracting
  discussionNode type, mapDiscussionNodesToIssues helper, and separate
  query functions for with/without category filter

Co-Authored-By: Shannon Hittson <shannon.hittson@cognition.ai>
@devin-ai-integration
Copy link
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@sonarqubecloud
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants